/**
 * 新开页面不关闭时，的打印
 */
function printIm(flag,list11)
{
	var cbm01Arr=[];
	var cbm01Arr1=[];
	var list = list11;
	if (flag)// 如果打勾需要直接调用打印服务
	{
		printImByRids(list11);
	}
}
//一次性请求打印格式后直接打印
function printImByRids(list){
	debugger;
	var rids = [],printList=[];
	for (var i = 0; i < list.length; i++)
	{
		if (judgeFlag(list[i].bda01, list[i].vaf01a, list[i].vaf32))
		{
			var rid = getRids(list[i]);
			rids.push(rid);
			printList.push(list[i]);
		}
		//输液
		if(list[i].bbx20=="2" || list[i].bbx20=="1")
		{
			var rid = getSyRids(list[i]);
			rids.push(rid);
			printList.push(list[i]);
		}
		//皮试
//		if(list[i].vaf23=="皮试( )")
//		{
//			printList.push(list[i]);
//		}
	}
	rids = rids.unique2();//去重
	$.when(
		getAllPrintType(rids)
	 ).done(function (data) {
		 //组装打印的数组
		 configPrintList(printList,data);
	        	
	 });
}
function configPrintList(list,data){
	debugger;
	var printArr= [];
	var shuyeArr=[];
	var pishiArr=[];
	var cbm01Arr=[];
	var cbm01Arr1=[];
	for (var i = 0; i < list.length; i++)  //list数据列表
	{
		var defaultV2;
		for (var j = 0; j< data.length; j++) //data为请求回来的打印格式列表
		{
			var rid = getRids(list[i]);
			if(rid == data[j].reportId){
				if (data[j]['xdefault'] == "1"){   //匹配对应的数据和格式
					
					defaultV2 = data[j].id;
					list[i].defaultV2 = defaultV2;
					if (judgeFlag(list[i].bda01, list[i].vaf01a, list[i].vaf32))
					{
						printArr.push(list[i]);
					}
					//输液
					if(list[i].bbx20=="2" || list[i].bbx20=="1")
					{
						shuyeArr.push(list[i]);
					}
					//皮试
					if(list[i].vaf23=="皮试( )")
					{
						pishiArr.push(list[i]);
					}
					
				}
				
			}
		}
	}
	//处方单
	for (var k = 0; k < printArr.length; k++)
	{
		if($.inArray(printArr[k].cbm01,cbm01Arr)<0)
		{
			send_print(printArr[k].defaultV2, printArr[k]['cbm01']);
			cbm01Arr.push(printArr[k].cbm01);
		}
	}
	if (valueArr['p_9999_9999_98']=="1")
	{
		//输液单
		for (var j = 0; j < shuyeArr.length; j++)
		{
			if($.inArray(shuyeArr[j].cbm01,cbm01Arr1)<0)
			{
				send_print(shuyeArr[j].defaultV2, shuyeArr[j]['cbm01']);
				cbm01Arr1.push(shuyeArr[j].cbm01);
			}
		}
		//皮试单 --- 深圳宝田医院暂定不打印
		/*for (var k = 0; k < shuyeArr.length; k++)
		{
			send_print(12, printArr[k]['cbm01']);
		}*/
	}
	printArr.length = 0;
	shuyeArr.length = 0;
	pishiArr.length = 0;
}
//批量查询打印格式，直接打印
function getAllPrintType(rids){
	debugger;
    var def = $.Deferred();
    $.ajax({
    	url: printUrl + "/printtemprest/findListByRids?jsessionids=" + jsessionids,
    	data:{'rids':rids},
    	dataType: "json",
    	type:"POST",
		traditional : true,
    	success: function (resp) {
    		def.resolve(resp);
    	}
    });
    return def.promise();
}
//rid 查询
function getRids(dataObj){
	var bda01 = dataObj.bda01;
	var rid;
	switch (bda01)
	{
		case "1" :
			rid = justXiyaoType1(dataObj.cbm07,rid);
			break;
		case "2" :
			rid = justXiyaoType1(dataObj.cbm07,rid);
			break;
		case "8" :
			rid = 25;
			break;
		case "E" :
			rid = 22;
			break;
		case "L" :
			rid = 23;
			break;
		case "S" :
			rid = 28;
			break;
		case "4" :
			rid = 190;//待定
			break;
		case "M" :
			rid = 190;//待定
			break;
		default ://治疗
			rid = 190;
			break;
	}
	return rid;
}
function getSyRids(dataObj){
	var rid;
	var  bbx20 = dataObj.bbx20;
	switch (bbx20){
		case 1 :
			rid = 101;
			break;
		case 2 :
			rid = 101;
			break;
	}
	return rid;
}
function judgeFlag(bda01, vaf01a, vaf32)
{
	var flag = false;
	if ("128".indexOf(bda01) >= 0)
	{
		flag = true;
	}
	if ("SE".indexOf(bda01) >= 0 && vaf01a == 0)
	{
		flag = true;
	}
	if ("1238SE".indexOf(bda01) < 0 && vaf01a == 0 && vaf32 == 0)
	{
		flag = true;
	}
	return flag;
}

function send_print(id, cbm01)
{
	var url = printUrl + "/printtoolrest/printToPdf/" + id + "?cbm01=" + cbm01;
	layer.open(
	{
	type : 2,
	title : false, // title 不显示
	closeBtn : 0, // 不显示 layer 弹框关闭按钮
	shadeClose : false,
	shade : false,
	area : [
	'0%', '0%'
	], // 在页面上不显示
	content : url, // iframe的url,
	success : function()
	{
		
	},
	error:function(a,b,c)
	{
		layer.msg(c, {icon: 2,time:2000,skin: 'layui-layer-msg2'});   
	}
	});
}
function justXiyaoType1(cbm07,rid){
	//普通、急诊
	if(cbm07 == "1"||cbm07 == "2"||(cbm07 == "6")){
		rid = 24;
	}
	else if(cbm07 == "3"){ //儿童
		rid = 103;
	}
	//毒麻
	else if((cbm07 == "4")||(cbm07 == "5")||(cbm07 == "8")){
		rid = 89;
	}
	return rid;
}